package net.newsmth.smthapp.android.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;

import net.newsmth.smthapp.android.R;
import net.newsmth.smthapp.android.adapter.HotFragmentAdapter;

import net.newsmth.smthapp.android.config.SmthConnection;
import net.newsmth.smthapp.android.helper.SectionHelper;
import net.newsmth.smthapp.android.helper.SectionHelperImpl;
import net.newsmth.smthapp.android.model.BBSThread;
import net.newsmth.smthapp.android.widget.PullToRefreshLayout;


/**
 * Created by Hatsune Miku on 4/11/14.
 */
public class HotFragment extends Fragment implements PullToRefreshLayout.PullToRefreshListener {
    private ExpandableListView listView;
    private PullToRefreshLayout pullToRefreshLayout;
    private View parentView;
    private HotFragmentAdapter adapter;
    private int expandGroup = -1;

    private Looper looper;

    public static String EXPANDED_GROUP_KEY = "EXPANDED_GROUPS";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        expandGroup = getArguments().getInt(EXPANDED_GROUP_KEY);

        looper = Looper.getMainLooper();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        parentView = inflater.inflate(R.layout.fragment_hot, null);
        pullToRefreshLayout = (PullToRefreshLayout)parentView.findViewById(R.id.pullToRefreshLayout);

        initializeExpandableListView(inflater);

        return parentView;
    }

    private void initializeExpandableListView(LayoutInflater inflater) {
        listView = (ExpandableListView)parentView.findViewById(R.id.hot_list_view);
        pullToRefreshLayout.setListView(listView);
        pullToRefreshLayout.setPullToRefreshListener(this);
        listView.setOnChildClickListener(new HotFragmentOnChildClickListener());

        SectionHelper helper= new SectionHelperImpl(SmthConnection.getCurrent());
        adapter = new HotFragmentAdapter(inflater.getContext(), helper);
        listView.setAdapter(adapter);

        if (expandGroup != -1) {
            listView.expandGroup(expandGroup);
        }
    }

    @Override
    public void onRefresh() {

        Handler handler = new Handler(looper);
        handler.post(new Runnable() {
            @Override
            public void run() {
                HotFragment.this.adapter.refresh();
                int groupCount = adapter.getGroupCount();

                for (int i = 0; i < groupCount; i++) {
                    if (listView.isGroupExpanded(i)) {
                        listView.collapseGroup(0);
                        listView.expandGroup(0);
                    }
                }
                pullToRefreshLayout.finishRefreshing();
            }
        });
    }

    private class HotFragmentOnChildClickListener implements ExpandableListView.OnChildClickListener {

        @Override
        public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long id) {
            ExpandableListAdapter adapter = (ExpandableListAdapter)expandableListView.getExpandableListAdapter();

            BBSThread thread = (BBSThread)adapter.getChild(groupPosition, childPosition);

            Intent intent = new Intent(HotFragment.this.getActivity(), ArticleListActivity.class);
            intent.putExtra(ArticleListActivity.BBSThread_KEY, thread);
            intent.putExtra(ArticleListActivity.BOARD_NAME_KEY, thread.getBoard());
            intent.putExtra(ArticleListActivity.THREAD_ID_KEY, thread.getId());
            startActivity(intent);

            return true;
        }
    }
}
